<?php
function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0,$timeout=5,$location=0){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	$httpheader[] = "Accept: */*";
	$httpheader[] = "Accept-Encoding: gzip,deflate,sdch";
	$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
	$httpheader[] = "Connection: close";
	$httpheader[] = "x-requested-with: XMLHttpRequest";
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	if($post){
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		$httpheader[] = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8";
	}
	curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
	if($header){
		curl_setopt($ch, CURLOPT_HEADER, TRUE);
	}
	if($cookie){
		curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	}
	if($referer){
		if($referer==1){
			curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
		}else{
			curl_setopt($ch, CURLOPT_REFERER, $referer);
		}
	}
	if($ua){
		curl_setopt($ch, CURLOPT_USERAGENT,$ua);
	}else{
		curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4355.400 QQBrowser/9.7.12672.400');
	}
	if($nobaody){
		curl_setopt($ch, CURLOPT_NOBODY,1);
	}
	curl_setopt($ch, CURLOPT_ENCODING, "gzip");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	if($location)curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 2);
	$ret = curl_exec($ch);
	curl_close($ch);
	return $ret;
}

if($_GET['action']=='getjs'){
    @header('Content-Type: application/javascript; charset=UTF-8');
    $uid = $_REQUEST['uid'];
    $data=get_curl('https://www.iesdouyin.com/share/user/'.$uid,0,0,0,0,$_SERVER['HTTP_USER_AGENT'],0,0,1);
    preg_match("/tac='(.*?)'/", $data, $res);
    $tac = $res[1];
    preg_match("/dytk:.*'(.*)'/", $data, $res);
    $dytk = $res[1];
    $file=str_replace('{$UA}',$_SERVER['HTTP_USER_AGENT'],file_get_contents('douyin_fun.js'));
    $file=str_replace('{$uid}',$uid,$file);
    $file=str_replace('{$tac}',$tac,$file);
    $file=str_replace('{$dytk}',$dytk,$file);
    exit($file);
}elseif($_GET['action']=='getlist'){
    $uid=$_POST['uid'];
    $dytk=$_POST['dytk'];
    $signature=$_POST['signature'];
    $ua=$_SERVER['HTTP_USER_AGENT'];
    $data=get_curl('https://www.douyin.com/aweme/v1/aweme/post/?user_id='.$uid.'&count=80&max_cursor=0&aid=1128&_signature='.$signature.'&dytk='.$dytk,0,0,0,0,$ua);
    $arr=json_decode($data,true);
    if($arr['aweme_list']){
        $code=0;
        $msg='获取成功';
    }else{
        $code=1;
        $msg='获取失败，请重新复制抖音链接';
    }
    $result=array('code'=>$code,'data'=>$arr['aweme_list'],'msg'=>$msg,'url'=>'https://www.douyin.com/aweme/v1/aweme/post/?user_id='.$uid.'&count=80&max_cursor=0&aid=1128&_signature='.$signature.'&dytk='.$dytk,'uid'=>$uid);
    exit(json_encode($result));
}
